package org.sosie2.service.audit;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.HashMap;
import java.util.Vector;

import org.apache.axis.Message;
import org.apache.axis.MessageContext;
import org.apache.axis.message.SOAPEnvelope;
import org.opensaml.SAMLException;
import org.sosie2.service.framework.AbstractService;
import org.sosie2.service.framework.SAMLAnnotation;
import org.sosie2.service.framework.Utils;

/**
 *
 * This service logs soap messages following into a file.
 * Afterwards, the messages stored on the disk in this way can
 * be further processed (audit).
 * 
 *  @author Gabriela
 */
public class LoggerService extends AbstractService
{
	public static String outputFile = "";
	
	/**
	 * Simple method to initialize all parameters: the output file used.
	 */
	public void init()
	{
		outputFile =  Utils.LOG;
	}
	
	/**
	 * The service generates no annotation to soap message, so the body of this function is null.
	 */
	public SAMLAnnotation generateAnnotation(String actor, String namespace, HashMap<String,Vector<String>> attributeNamesToValues , String target) throws SAMLException
	{ 
		return null;
	}
	
	public LoggerService()
	{
		init();
	}
	
	/**
	 * This is the main method exposed by the web service. It receives a SOAPEnvelope 
	 * and depending on the formated contents, it dispatches the messages
	 * to the specified file
	 */
	public Message invoke(Message req, Message resp) throws Exception {
		FileWriter fw = new FileWriter(outputFile, true);			
        BufferedWriter out = new BufferedWriter(fw);	        
        out.write(req.getSOAPPartAsString() + "/n/n");	        
        out.close();
	    
        return req;	 	
	}

}


